﻿using SHKF.B2C.Mannage.AdminUI.Model;
using SHKF.B2C.Model.Dto;
using SHKF.B2C.Model.Dto.Center;
using SHKF.B2C.Model.Entity.Center;
using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using WQDSkin.Forms.Component;
using WQDSkin.Forms.Controls; 

namespace SHKF.B2C.Mannage.AdminUI.View.CenterInfo.Product
{
    /// <summary>
    /// frmProduct.xaml 的交互逻辑
    /// </summary>
    public partial class frmProduct : BasePage
    {
        public frmProduct()
        {
            InitializeComponent();
        }

        public override void CallBack(object model)
        {
            base.CallBack(model);

            LoadProduct();
        }
         
        private void btnSearchCompany_Click(object sender, RoutedEventArgs e)
        {
            //加载公司信息
            LoadCompany();
        }

        private void btnSearchProdct_Click(object sender, RoutedEventArgs e)
        {

            LoadProduct();
        }

        private void BasePage_Loaded(object sender, RoutedEventArgs e)
        {
            LoadCompany();
        }

        private async void LoadCompany()
        {
            string CompanyName = this.txtCompany.Text;
            CompanyListDto dto = await GL.HttpClientManage.QueryCompanyList(CompanyName);
         
            TreeViewItem tviRoot = (TreeViewItem)this.tvCompany.Items[0];
            tviRoot.ItemsSource = null;

            if (dto.error == null)
            {
               foreach(CompanyEntity company in dto.data)
                {
                    WQDTreeViewItem treeViewItem = new WQDTreeViewItem();
                    treeViewItem.DataContext = company;
                    treeViewItem.Header = company.CompanyName;
                    tviRoot.Items.Add(treeViewItem);
                } 
                tviRoot.IsExpanded = true;
            }
            else
            {
                MessageBox.Show(dto.error.msg);
            }
        }
        private async void LoadProduct()
        { 
            this.lblCompany.Content = SelectedText;

            string ProductName = this.txtProduct.Text;
            ProductListDto dto = await GL.HttpClientManage.QueryProduct(SelectedCompanSN, SelectedShopSN, ProductName);
            this.dgv.ItemsSource = null;
            if (dto.error == null)
            {
                this.dgv.ItemsSource = dto.data;
            }
            else
            {
                MessageBox.Show(dto.error.msg);
            } 
        }

        private void btnEdit_Click(object sender, RoutedEventArgs e)
        {
            if (this.dgv.SelectedItems.Count > 0)
            {
                ProductInfoEntity shop = (ProductInfoEntity)this.dgv.SelectedValue;
                base.Contain.Navigate("/View/CenterInfo/Product/frmProductEdit", shop.Clone());
            }
        }
 
        /// <summary>
        /// 删除虚拟产品
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private async void btnDelete_Click(object sender, RoutedEventArgs e)
        {
            if (this.dgv.SelectedItems.Count == 0)
            {
                MessageBox.Show("请选择数据");
                return;
            }

            List<ProductInfoEntity> products = new List<ProductInfoEntity>();
            foreach (ProductInfoEntity info in this.dgv.SelectedItems)
            {
                products.Add(info);
            }

            DataDto dto = await GL.HttpClientManage.BatchDeleteProduct(products);
            if (dto.error == null)
            {
                MessageBox.Show("删除成功!");
                this.LoadProduct();
            }
            else
            {
                MessageBox.Show(dto.error.msg);
            }

        }

        private void dgv_LoadingRow(object sender, DataGridRowEventArgs e)
        {
            e.Row.Header = e.Row.GetIndex() + 1;
        }
 

        private async void tvCompany_MouseDoubleClick(object sender, MouseButtonEventArgs e)
        {
            if(this.tvCompany.SelectedItem != null)
            {
                WQDTreeViewItem itemSelected = (WQDTreeViewItem)this.tvCompany.SelectedItem;
                if(itemSelected.DataContext is CompanyEntity && itemSelected.Items.Count == 0)
                {
                    int companySN = (int)((CompanyEntity)itemSelected.DataContext).SN; 
                    if (companySN > 0)
                    {
                        ShopListDto dto = await GL.HttpClientManage.ShopQuery(companySN, "");
                        if (dto.error == null)
                        {
                            foreach(var shop in dto.data)
                            {
                                WQDTreeViewItem item = new WQDTreeViewItem();
                                item.Header = shop.ShopName;
                                item.DataContext = shop;
                                itemSelected.Items.Add(item);
                            } 
                        }
                        else
                        {
                            MessageBox.Show(dto.error.msg);
                        }
                    }
                }
            }
        }

        private long SelectedCompanSN
        {
            get
            {
                if (this.tvCompany.SelectedItem != null)
                {
                    WQDTreeViewItem itemSelected = (WQDTreeViewItem)this.tvCompany.SelectedItem;
                    if (itemSelected.DataContext is CompanyEntity)
                    {
                        int companySN = (int)((CompanyEntity)itemSelected.DataContext).SN;
                        return companySN;
                    }
                    else if (itemSelected.DataContext is ShopEntity)
                    {
                        int companySN = (int)((ShopEntity)itemSelected.DataContext).CompanySN;
                        return companySN;
                    }
                }
                return 0;
            }
        }

        private long SelectedShopSN
        {
            get
            {
                if (this.tvCompany.SelectedItem != null)
                {
                    WQDTreeViewItem itemSelected = (WQDTreeViewItem)this.tvCompany.SelectedItem; 
                    if (itemSelected.DataContext is ShopEntity)
                    {
                        int ShopSN = (int)((ShopEntity)itemSelected.DataContext).ShopSN;
                        return ShopSN;
                    }
                }
                return 0;
            }
        }

        private string SelectedText
        {
            get
            {
                if (this.tvCompany.SelectedItem != null)
                {
                    WQDTreeViewItem itemSelected = (WQDTreeViewItem)this.tvCompany.SelectedItem;
                    if (itemSelected.DataContext is CompanyEntity)
                    {
                        string CompanyName = ((CompanyEntity)itemSelected.DataContext).CompanyName;
                        return CompanyName;
                    }
                    else if (itemSelected.DataContext is ShopEntity)
                    {
                        string ShopName =((ShopEntity)itemSelected.DataContext).ShopName;
                        return ShopName;
                    }
                }
                return "";
            }
        }

        private void btnAddFrom_Click(object sender, RoutedEventArgs e)
        {
            //从商品库中获取

        }
    }
}
